/*
 * StartovniKategorieDialog.java
 *
 * Created on 2. leden 2007, 16:09
 */

package cz.results.winter6.view.dialogs.stkategorie;

import cz.results.winter6.model.Filtr;
import cz.results.winter6.model.KategorieConfig;
import cz.results.winter6.model.KategorieConfigList;
import cz.results.winter6.model.StartovniCasConfig;
import cz.results.winter6.model.slmodel.StartListModel;
import cz.results.winter6.view.dialogs.filtrdialog.FiltryDialog;
import java.util.Iterator;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JDialog;


public class StartovniKategorieDialog extends JDialog {
    
    private StartovniCasConfigTableModel tm;
    /** true - dochazi k porizovani noveho zanznamu, false - editace uz vlozeneho zaznamu */
    private boolean novyZaznam;
    
    private StartListModel startListModel;
    
    private KategorieConfigList kategorieConfigList;
    private KategorieConfig kategorieConfig;
    
    public StartovniKategorieDialog(JDialog parent, boolean modal, StartListModel startListModel, String nazevTypuKategorie, int indexKategorieConfig, boolean novyZaznam) {
        super(parent, modal);
        
        setStartListModel(startListModel);
        setNovyZaznam(novyZaznam);
        
        kategorieConfigList = getKategorieConfigList(nazevTypuKategorie);
        
        if(isNovyZaznam()){
            //pokud je novy zaznam musim vytvorit novy KategorieConfig
            kategorieConfig = new KategorieConfig(getKategorieConfigList().getPocetJizd());
        } else {
            kategorieConfig = getKategorieConfigList().get(indexKategorieConfig);
        }
        
        setTm(new StartovniCasConfigTableModel(getKategorieConfig()));
        
        
        initComponents();
        
        jTextFieldNazevKategorie.setText(getKategorieConfig().getNazev());
        jTextFieldDatum.setText(getKategorieConfig().getDatum());
        jTextFieldZkratka.setText(getKategorieConfig().getZkratka());
        jTextFieldBarvaCisel.setText(getKategorieConfig().getBarvaCisel());
        jTextFieldPoradiPodleJizdy.setText(String.valueOf(getKategorieConfig().getJizdaPoradi()));
        
        naplnJComboBox();
    }
    
    public void naplnJComboBox(){
        jComboBoxFiltr.setModel(new DefaultComboBoxModel());
        
        Iterator i = getStartListModel().getZavod().getFiltry().iterator();
        
        while(i.hasNext()){                              //naplni combo vsema filtrama
            Filtr filtr= (Filtr) i.next();
            ((DefaultComboBoxModel)jComboBoxFiltr.getModel()).addElement(filtr);
        }
        
        jComboBoxFiltr.setSelectedItem(getKategorieConfig().getFiltr());     //vybere prislusny filtr kategorie
    }
    
    public KategorieConfigList getKategorieConfigList(String nazevTypuKategorie){
        return getStartListModel().getZavod().getKategorieConfigList(nazevTypuKategorie);
    }
    
    public StartovniCasConfigTableModel getTm() {
        return tm;
    }
    
    public void setTm(StartovniCasConfigTableModel tm) {
        this.tm = tm;
    }
    
    
    public boolean isNovyZaznam() {
        return novyZaznam;
    }
    
    public void setNovyZaznam(boolean novyZaznam) {
        this.novyZaznam = novyZaznam;
    }
    
    public StartListModel getStartListModel() {
        return startListModel;
    }
    
    public void setStartListModel(StartListModel startListModel) {
        this.startListModel = startListModel;
    }
    
    public KategorieConfigList getKategorieConfigList() {
        return kategorieConfigList;
    }
    
    public KategorieConfig getKategorieConfig() {
        return kategorieConfig;
    }
    
    
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jTextFieldNazevKategorie = new javax.swing.JTextField();
        jComboBoxFiltr = new javax.swing.JComboBox();
        jButtonFiltryDialog = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldZkratka = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldBarvaCisel = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldPoradiPodleJizdy = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButtonOk = new javax.swing.JButton();
        jButtonZrus = new javax.swing.JButton();
        jButtonUprav = new javax.swing.JButton();
        jButtonNasadCasy = new javax.swing.JButton();
        jTextFieldDatum = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();

        setTitle("konfigurace startovn\u00ed kategorie");
        setLocationByPlatform(true);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        jTextFieldNazevKategorie.setNextFocusableComponent(jTextFieldZkratka);

        jComboBoxFiltr.setNextFocusableComponent(jButtonFiltryDialog);

        jButtonFiltryDialog.setText("uprav");
        jButtonFiltryDialog.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonFiltryDialogActionPerformed(evt);
            }
        });

        jLabel1.setText("n\u00e1zev kategorie");

        jLabel2.setText("zkratka");

        jTextFieldZkratka.setNextFocusableComponent(jTextFieldBarvaCisel);

        jLabel3.setText("barva \u010d\u00edsel");

        jTextFieldBarvaCisel.setNextFocusableComponent(jTextFieldPoradiPodleJizdy);

        jLabel4.setText("po\u0159ad\u00ed podle j\u00edzdy");

        jTextFieldPoradiPodleJizdy.setNextFocusableComponent(jComboBoxFiltr);

        jTable1.setModel(tm);
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });

        jScrollPane1.setViewportView(jTable1);

        jButtonOk.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cz/results/winter6/view/icons/button_ok.png")));
        jButtonOk.setText("ok");
        jButtonOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOkActionPerformed(evt);
            }
        });

        jButtonZrus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cz/results/winter6/view/icons/button_cancel.png")));
        jButtonZrus.setText("zru\u0161");
        jButtonZrus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonZrusActionPerformed(evt);
            }
        });

        jButtonUprav.setText("uprav");
        jButtonUprav.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonUpravActionPerformed(evt);
            }
        });

        jButtonNasadCasy.setText("nasad casy");
        jButtonNasadCasy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNasadCasyActionPerformed(evt);
            }
        });

        jLabel5.setText("datum");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(110, 110, 110))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jTextFieldNazevKategorie, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addGap(6, 6, 6))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jTextFieldDatum, javax.swing.GroupLayout.DEFAULT_SIZE, 203, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jTextFieldZkratka, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(16, 16, 16)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jTextFieldBarvaCisel, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 84, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel4)
                            .addComponent(jTextFieldPoradiPodleJizdy, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBoxFiltr, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(16, 16, 16)
                        .addComponent(jButtonFiltryDialog)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGap(28, 28, 28))
            .addGroup(layout.createSequentialGroup()
                .addGap(199, 199, 199)
                .addComponent(jButtonOk, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonZrus)
                .addContainerGap(279, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButtonUprav)
                .addGap(18, 18, 18)
                .addComponent(jButtonNasadCasy)
                .addContainerGap(446, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5)
                .addContainerGap(595, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 623, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addGap(2, 2, 2)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldNazevKategorie, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldZkratka, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldBarvaCisel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldPoradiPodleJizdy, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldDatum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButtonFiltryDialog)
                            .addComponent(jComboBoxFiltr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(15, 15, 15)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonUprav)
                    .addComponent(jButtonNasadCasy))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonOk)
                    .addComponent(jButtonZrus))
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents
        
    private void jButtonNasadCasyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNasadCasyActionPerformed
        getKategorieConfig().setFiltr(((Filtr)jComboBoxFiltr.getSelectedItem()));     //musim ulozit filtr do kategorie, aby mel ucinnost
        getStartListModel().nasadStartovniCasyKategorii(getKategorieConfig(), getStartListModel().getZavod().getSl());
        getTm().fireTableDataChanged();
    }//GEN-LAST:event_jButtonNasadCasyActionPerformed
    
    
    private void upravaStartovnihoCasu(){
        final JDialog parent = this;
        
        int selectedRow = jTable1.getSelectedRow();
        
        if(selectedRow!=-1) {                            //musi byt vybran nejaky radek
            
            final StartovniCasConfig startovniCasConfig = getKategorieConfig().getStartovniCasConfigList().get(selectedRow);
            
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new StartovniCasDialog(parent, true, startovniCasConfig).setVisible(true);
                    getTm().fireTableDataChanged();
                }
            });
        }
    }
    
    
    
    private void jButtonUpravActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonUpravActionPerformed
        upravaStartovnihoCasu();
    }//GEN-LAST:event_jButtonUpravActionPerformed
    
    private void jButtonZrusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonZrusActionPerformed
        setVisible(false);
        dispose();
    }//GEN-LAST:event_jButtonZrusActionPerformed
    
    private void jButtonOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOkActionPerformed
        getKategorieConfig().setNazev(jTextFieldNazevKategorie.getText().trim());
        getKategorieConfig().setDatum(jTextFieldDatum.getText().trim());
        getKategorieConfig().setBarvaCisel(jTextFieldBarvaCisel.getText().trim());
        getKategorieConfig().setJizdaPoradi(Integer.valueOf(jTextFieldPoradiPodleJizdy.getText().trim()));
        getKategorieConfig().setZkratka(jTextFieldZkratka.getText().trim());
        getKategorieConfig().setFiltr((Filtr)jComboBoxFiltr.getSelectedItem());
        
        if(novyZaznam){
            getKategorieConfigList().add(getKategorieConfig());
        }
        setVisible(false);
        dispose();
    }//GEN-LAST:event_jButtonOkActionPerformed
    
    private void jButtonFiltryDialogActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonFiltryDialogActionPerformed
        final JDialog parent = this;
        
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FiltryDialog(parent, true, getStartListModel().getZavod().getFiltry()).setVisible(true);
                naplnJComboBox();
            }
        });
    }//GEN-LAST:event_jButtonFiltryDialogActionPerformed
    
    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        if(evt.getClickCount()==2){
            upravaStartovnihoCasu();
        }
    }//GEN-LAST:event_jTable1MouseClicked
    
    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonFiltryDialog;
    private javax.swing.JButton jButtonNasadCasy;
    private javax.swing.JButton jButtonOk;
    private javax.swing.JButton jButtonUprav;
    private javax.swing.JButton jButtonZrus;
    private javax.swing.JComboBox jComboBoxFiltr;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextFieldBarvaCisel;
    private javax.swing.JTextField jTextFieldDatum;
    private javax.swing.JTextField jTextFieldNazevKategorie;
    private javax.swing.JTextField jTextFieldPoradiPodleJizdy;
    private javax.swing.JTextField jTextFieldZkratka;
    // End of variables declaration//GEN-END:variables
    
}
